package org.web.ssm.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.web.ssm.dao.ProductMapper;
import org.web.ssm.model.Product;
import org.web.ssm.model.queryVo.BaseQuery;
import org.web.ssm.service.IProductService;


/**
 * Created by hubin on 2016/6/16.
 */
@Service("iProuctService")
public class ProuctServiceimpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public BaseQuery<Product> getProductList(Integer managerId, int pageSize, int pageCurrent,String keyword) throws Exception {
        BaseQuery<Product> productQuery = new BaseQuery<>();
        productQuery.setPageSize(pageSize);
        productQuery.setMaxRow(pageSize * pageCurrent + 1);
        productQuery.setMinRow(pageSize * (pageCurrent -1));
        productQuery.setCurrentPage(pageCurrent);
        productQuery.setIntkeyWords(managerId);
        productQuery.setKeyWords(keyword);
        productQuery.setResults(productMapper.getProductList(productQuery));
        productQuery.setTotalRow(productMapper.getCount(productQuery));
        int totalPage = productQuery.getTotalRow() % productQuery.getPageSize()  == 0 ? productQuery.getTotalRow() / productQuery.getPageSize():productQuery.getTotalRow() / productQuery.getPageSize() +1;
        productQuery.setTotalPage(totalPage);
        return productQuery;
    }

    @Override
    public void addProduct(Product product) throws Exception {
        productMapper.addProduct(product);
    }

    @Override
    public void updateProduct(Product product) throws Exception {
        productMapper.updateProduct(product);
    }

    @Override
    public void delProduct(int id) throws Exception {
        productMapper.delProduct(id);
    }

    @Override
    public void tookProduct(int id) throws Exception {
        productMapper.tookProduct(id);
    }
}
